package jackson;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Test {
	public static void main(String[] args) throws Exception {
		Address address = new Address(1, "address");
		Customer customer = new Customer(1, "jason", new Date(), 23.45,address);
		
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.writeValue(System.out, customer);	//write to console
		
		OutputStream outputStream = new FileOutputStream("test.json");
		objectMapper.writeValue(outputStream, customer);	//write to file
		
//		Customer customer2 = new Customer(2, "jason", new Date(), 23.45,address);
//		objectMapper.writeValue(outputStream, customer2);	//没办法重复写，会覆盖
		
		
		//从文件中读到JSON string，转为对应的object
		Customer c1 = objectMapper.readValue(new FileInputStream("test.json"), Customer.class);
		System.out.println(c1);
	}

}
